import { ErrorMessageMode } from '/#/axios';
import { toolsHttp as defHttp } from '/@/utils/http';
import type {
  GameId,
  DeckConfigData,
  CreateDeck,
  DeckDetail,
  DeckList,
  DeckListQuery,
  DeckCardDetail,
} from './model/deckModel';
import { isArray } from '/@/utils/is';

enum Api {
  DeckConfig = '/app/card-deck/config',
  DeckList = '/app/card-deck/self-list',
  DeckDetail = '/app/card-deck/info',
  DeckImageDetail = '/app/card-deck/share-img-info',
  ModifyDeck = '/app/card-deck/update',
  CreateDeck = '/app/card-deck/create',
  DeleteDeck = '/app/card-deck/delete',
  ExportDeckImg = '/app/card-deck/export-deck-img',
  DeckCardDetail = '/app/card-deck/card-info',
}

// 获取套牌配置
export function DeckConfigApi(params: GameId, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckConfigData>(
    {
      url: Api.DeckConfig,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取套牌列表
export function DeckListApi(params: DeckListQuery, model: ErrorMessageMode = 'none') {
  return defHttp.post<DeckList>(
    {
      url: Api.DeckList,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取套牌详情
export function DeckDetailApi(params: Pick<DeckDetail, 'deck_id'>, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckDetail>(
    {
      url: Api.DeckDetail,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 导出图片用套牌详情
export function DeckImageDetailApi(params: Pick<DeckDetail, 'deck_id'>, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckDetail>(
    {
      url: Api.DeckImageDetail,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 修改套牌
export function ModifyDeckApi(params: CreateDeck, model: ErrorMessageMode = 'dialog') {
  const jsonParam: any = {
    deck_id: params.deck_id,
    deck_name: params.deck_name,
    // deck_info: params.deck_info ? JSON.stringify(params.deck_info) : '[]',
    deck_desc: params.deck_desc,
    is_open: params.is_open || 0,
    tags: isArray(params.tags) ? params.tags.join(',') : '',
  };
  if (params.deck_info) {
    jsonParam.deck_info = JSON.stringify(params.deck_info);
  }
  return defHttp.post(
    {
      url: Api.ModifyDeck,
      data: jsonParam,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 创建套牌
export function CreateDeckApi(params: CreateDeck, model: ErrorMessageMode = 'dialog') {
  const jsonParam = {
    game_id: params.game_id,
    deck_name: params.deck_name,
    deck_info: params.deck_info ? JSON.stringify(params.deck_info) : '[]',
  };
  return defHttp.post(
    {
      url: Api.CreateDeck,
      data: jsonParam,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 删除套牌
export function DeleteDeckApi(params: number[], model: ErrorMessageMode = 'notify') {
  const jsonParam = {
    deck_ids: params.join(','),
  };
  return defHttp.post(
    {
      url: Api.DeleteDeck,
      data: jsonParam,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 导出套牌图片
export function ExportDeckImgApi(
  params: Pick<DeckDetail, 'deck_id'> & { type: number },
  model: ErrorMessageMode = 'dialog'
) {
  return defHttp.post<string>(
    {
      url: Api.ExportDeckImg,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取卡牌详情
export function DeckCardDetailApi(card_id: number, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckCardDetail>(
    {
      url: Api.DeckCardDetail,
      data: { card_id: card_id },
    },
    {
      errorMessageMode: model,
    }
  );
}
